﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using ARS.Control.Custom.Entity.Fields;

namespace ARS.Control.Custom.Entity.Forms
{
    public class BooleanForm:Form
    {
        protected override void RenderContents(System.Web.UI.HtmlTextWriter output)
        {
            base.RenderContents(output);
            BooleanField field = base.Field as BooleanField;
            string html=null;
            if (field.Value == null)
            {
                if (field.DefaultValue == field.SelectValue)
                {
                    html = "<input id=\"{0}\" name=\"{0}\" type=\"checkbox\" checked=\"checked\" />";
                }
            }            
            else
            {
                string value = field.Value.ToString();
                if (value.ToLower() == "true" || value == field.SelectValue)
                {
                    html = "<input id=\"{0}\" name=\"{0}\" type=\"checkbox\" checked=\"checked\" />";
                }                
            }
            if (html == null)
            {
                html = "<input id=\"{0}\" name=\"{0}\"  type=\"checkbox\" />";
            }
            //html += base.Field;
            output.Write(string.Format(base.GetTrHtml(), string.Format(html,field.Html_Control_Name)));

            
        }

        public override object GetInputValue()
        {
            BooleanField field = base.Field as BooleanField;
            string b_v = HttpContext.Current.Request.Form[base.Field.Html_Control_Name];
            if (b_v == "on")
            {
                field.Value = field.SelectValue ?? "1";
            }
            else
            {
                field.Value = field.UnSelectValue ?? "0";
            }
            return field.Value;
        }
    }
}
